home *** CD-ROM | disk | FTP | other *** search
- Path: rain.fr!world-net!usenet
- From: Frederic LACHASSE <lachass@worldnet.fr>
- Newsgroups: comp.lang.c++
- Subject: Re: How to simulate EOF from the keyboard in Visual C++ 4.0, CTRL+Z doesn't work
- Date: Fri, 22 Mar 1996 23:13:01 +0000
- Organization: World-Net information exchange, Internet provider.
- Message-ID: <VA.00000071.00e2e9a9@fred>
- References: <314CB927.22A3@cco.caltech.edu>
- Reply-To: lachass@worldnet.fr
- NNTP-Posting-Host: client56.sct.fr
- X-Newsreader: Virtual Access by Ashmount Research Ltd, http://www.ashmount.com
-
- In article <314CB927.22A3@cco.caltech.edu>, Xuhua Li
- <xuhua@cco.caltech.edu> wrote:
- >
- > The title says everything. When I try to simulate the EOF from the
- > keyboard in Visual C++ with CTRL+Z, it doesn't work. CTRL+Z just kill
- > the running program. I am running windows 95.
- > Please try the following simple program from <<C++ Primer Plus>>:
- >
- > Program #1
- > // textin3.cpp -- reading chars to end of file
- > #include <iostream.h>
- > int main(void)
- > {
- > char ch;
- > int count = 0;
- >
- > while (cin.get(ch)) // cin.get(ch) is 0 on EOF
- > {
- > cout << ch;
- > count++;
- > }
- > cout << count << " characters read\n";
- > return 0;
- > }
- [second example snip]
-
- In fact, CTRL+Z have the expected result, the problem is that cout is
- buffered and Visual C++ (like most DOS C++ compilers) don't flush
- buffered iostreams, so your last message and the last entered line has
- few chances to be actually written to the screen.
-
- A buffered output stream is flushed when: the buffer is full, when an
- input is requested from a tied input stream (like cin and cout) or when
- it is explicitly flushed using:
-
- cout.flush();
-
- or:
-
- cout << flush;
-
- Very useful: the endl manipulator that send a new line to the output
- stream then flushes it.
-
- So, generally we do:
-
- cout << count << " characters read" << endl;
-
- Frederic LACHASSE (ECP 86)
- CompuServe: 100530,2005
- Internet: lachass@worldnet.fr
-
-